package cn.com.idmy.cloud.config;

import lombok.extern.slf4j.Slf4j;

import java.util.Properties;

@Slf4j
public class SpringEnvDruidProps {
    private static final String PREFIX = "spring.datasource.druid.";

    static void addProps(Properties props, DruidPoolConfig cfg) {
        // 连接池大小配置
        props.put("spring.datasource.type", "com.alibaba.druid.pool.DruidDataSource");
        props.put(PREFIX + "initial-size", cfg.getInitialSize());
        props.put(PREFIX + "min-idle", cfg.getMinIdle());
        props.put(PREFIX + "max-active", cfg.getMaxActive());

        // 获取连接配置
        props.put(PREFIX + "max-wait", cfg.getMaxWait());
        props.put(PREFIX + "not-full-timeout-retry-count", cfg.getNotFullTimeoutRetryCount());

        // 连接检测配置
        props.put(PREFIX + "validation-query", cfg.getValidationQuery());
        props.put(PREFIX + "test-while-idle", cfg.isTestWhileIdle());
        props.put(PREFIX + "test-on-borrow", cfg.isTestOnBorrow());
        props.put(PREFIX + "test-on-return", cfg.isTestOnReturn());
        props.put(PREFIX + "validation-query-timeout", 3);

        // 空闲连接回收配置
        props.put(PREFIX + "time-between-eviction-runs-millis", cfg.getTimeBetweenEvictionRunsMillis());
        props.put(PREFIX + "min-evictable-idle-time-millis", cfg.getMinEvictableIdleTimeMillis());
        props.put(PREFIX + "max-evictable-idle-time-millis", cfg.getMaxEvictableIdleTimeMillis());
        props.put(PREFIX + "keep-alive", cfg.isKeepAlive());
        props.put(PREFIX + "keep-alive-between-time-millis", cfg.getKeepAliveBetweenTimeMillis());

        // 连接泄露检测
        props.put(PREFIX + "remove-abandoned", true);
        props.put(PREFIX + "remove-abandoned-timeout", cfg.getRemoveAbandonedTimeout());
        props.put(PREFIX + "log-abandoned", cfg.isLogAbandoned());

        // PreparedStatement 池配置
        props.put(PREFIX + "pool-prepared-statements", cfg.isPoolPreparedStatements());
        props.put(PREFIX + "max-pool-prepared-statement-per-connection-size", cfg.getMaxPoolPreparedStatementPerConnectionSize());
        props.put(PREFIX + "share-prepared-statements", cfg.isSharePreparedStatements());

        // 查询超时配置
        props.put(PREFIX + "query-timeout", cfg.getQueryTimeout());
        props.put(PREFIX + "transaction-query-timeout", cfg.getTransactionQueryTimeout());

        // 初始化配置
        props.put(PREFIX + "async-init", cfg.isAsyncInit());
        props.put(PREFIX + "max-create-task-count", cfg.getMaxCreateTaskCount());

        // 监控配置
        props.put(PREFIX + "connection-properties", "druid.stat.mergeSql=true;druid.stat.slowSqlMillis=" + cfg.getSlowSqlMillis() + ";druid.stat.logSlowSql=true");
    }

    static void addProps(Properties props) {
        // 过滤器配置
        props.put(PREFIX + "filters", "stat,wall,slf4j");

        // 监控统计配置
        props.put(PREFIX + "stat.merge-sql", true);
        props.put(PREFIX + "stat.log-slow-sql", true);

        // Wall 防火墙配置
        props.put(PREFIX + "wall.config.multi-statement-allow", true);
        props.put(PREFIX + "wall.config.delete-allow", true);

        // 监控页面配置
        props.put(PREFIX + "stat-view-servlet.enabled", true);
        props.put(PREFIX + "stat-view-servlet.url-pattern", "/druid/*");
        props.put(PREFIX + "stat-view-servlet.reset-enable", true);
        props.put(PREFIX + "stat-view-servlet.login-username", "microhis-bcs");
        props.put(PREFIX + "stat-view-servlet.login-password", "fjaoifjoaeiwfoa3fwfafasdfdfas32r32fdsafdklsafdslaf##Rewfewfafllkdjsa");

        // Web 监控配置
        props.put(PREFIX + "web-stat-filter.enabled", true);
        props.put(PREFIX + "web-stat-filter.url-pattern", "/*");
        props.put(PREFIX + "web-stat-filter.exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        props.put(PREFIX + "web-stat-filter.profile-enable", true);
    }

}